#include "LineTool.h"
QColor LineTool::brushColor = Qt::blue;
int LineTool::brushSize = 2;

void LineTool::mousePressEvent(QMouseEvent* event, PaintArea *ourImage)
{
                        if(ourImage->getUndoPointer() == -1)
                                 ourImage->saveUndoState();
						originalImage = *ourImage->getImage();
                        ourImage->setLastPoint(event->pos());
                         ourImage->setScribbling(true);

}
void LineTool::mouseMoveEvent(QMouseEvent* event, PaintArea *ourImage)
{
		ourImage->setImage(originalImage);
       drawLineTo( event->pos(), ourImage);
}


void  LineTool::mouseReleaseEvent(QMouseEvent *event, PaintArea *ourImage)
 {
     if (event->button() == Qt::LeftButton ) {
                 
         drawLineTo(event->pos(), ourImage);

     }

 }

void LineTool::drawLineTo(const QPoint &endPoint, PaintArea *ourImage)
 {
	QPoint lastPoint = ourImage->getLastPoint();
	QImage* yup = ourImage->getImage();
	QPainter painter(yup);
	const QPen pen(brushColor, brushSize, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
	painter.setPen(pen);
         
	painter.drawLine(lastPoint, endPoint);
	//ourImage->modified = true;

	int rad = (brushSize / 2) + 2;
	ourImage->update(QRect(lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
	//ourImage->setLastPoint(endPoint);

        
 }
